﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_Article.Business
{
    public class DBAticleProperties
    {
        readonly LinqArticle db = new LinqArticle();

        public ud_ArticleProperty GetOne(Guid guid)
        {
            var result = from p in db.ud_ArticleProperties
                         where p.Guid == guid
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_ArticleProperty> GetAll(int moduleID)
        {
            var result = from p in db.ud_ArticleProperties
                         where p.ModuleID.Equals(moduleID)
                         orderby p.OrderNumber ascending
                         select p;
            return result.ToList();
        }

        public bool Delete(Guid guid)
        {
            bool flag = false;
            List<ud_ArticlePropertyValue> listValue =
                db.ud_ArticlePropertyValues.Where(p => p.PropertyGuid == guid).ToList();
            db.ud_ArticlePropertyValues.DeleteAllOnSubmit(listValue);
            ud_ArticleProperty item = GetOne(guid);
            if (item != null)
            {
                db.ud_ArticleProperties.DeleteOnSubmit(item);
            }
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool Create(int moduleID, string property, string unit, int orderNumber)
        {
            bool flag = false;

            ud_ArticleProperty item = new ud_ArticleProperty
                                          {
                                              ModuleID = moduleID,
                                              Guid = Guid.NewGuid(),
                                              Property = property,
                                              Unit = unit,
                                              OrderNumber = orderNumber
                                          };
            db.ud_ArticleProperties.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(Guid guid, string property, string unit, int orderNumber)
        {
            bool flag = false;
            ud_ArticleProperty item = GetOne(guid);
            if (item != null)
            {
                item.Property = property;
                item.Unit = unit;
                item.OrderNumber = orderNumber;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public void DeleteByModule(int moduleID)
        {
            List<ud_ArticleProperty> listItem = GetAll(moduleID);
            db.ud_ArticleProperties.DeleteAllOnSubmit(listItem);
            db.SubmitChanges();
        }
    }
}
